




//
//  JHSingleTitleGridView.m
//  JHSingleTitleGridView
//
//  Created by 307A on 16/9/25.
//  Copyright © 2016年 徐嘉宏. All rights reserved.
//

#import "JHGridView.h"
#import "JHGridTableViewCell.h"
#import "Model_Hr_InspectNew.h"
#import "UserDefault.h"
@interface JHGridView()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate,JHGDelegate>
@property (nonatomic) NSArray *titles;
@property (nonatomic) NSString *titleKey;


@property (nonatomic) NSArray *tags;
@property (nonatomic) NSArray *objects;

@property (nonatomic) NSArray *leftObjects;
@property (nonatomic) NSString *leftKey;



@property (nonatomic) UIScrollView *backScrollView;
@property (nonatomic) UIScrollView *backTitleScrollView;


@property (nonatomic,copy)NSString *dateString;

@property (nonatomic, assign) BOOL isSaved;

@property (nonatomic, assign) BOOL isHasInspect;

@property (nonatomic, copy)NSString *code_id;

@property (nonatomic, assign) NSInteger type;

@end

@implementation JHGridView
#pragma mark --Init Methods
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    return self;
}

- (void)setTitles:(NSArray *)titles
       andObjects:(NSArray *)objects
         withTags:(NSArray *)tags{
    if (titles.count != tags.count) {
    }
    _titles = titles;
    _tags = tags;
    _objects = objects;
    
    [self setupView];
}

- (void)configureViewWithTitlesArray:(NSArray *)titleObject
                            titleKey:(NSString *)titleKey
                           leftArray:(NSArray *)leftObject
                             leftKey:(NSString *)leftKey
                          itemsArray:(NSArray *)itemsObject
                            itemKeys:(NSArray *)itemKeys
                           restlt_id:(NSString *)result_id
                             isSaved:(BOOL)isSaved
                        isHasIpspect:(BOOL)isHasInspect
                             code_id:(NSString *)code_id
                                type:(NSInteger)type
{
    _titles = titleObject;
    _titleKey = titleKey;
    
    _leftObjects = leftObject;
    _leftKey = leftKey;
    
    _tags = itemsObject;
    _objects = itemKeys;
    
    self.dateString = result_id;
    self.isSaved = isSaved;
    self.isHasInspect = isHasInspect;
    self.code_id = code_id;
    
    self.type = type;
    
    [self setupView];
    
}

- (void)setupView{
    //remove subviews
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    
    //calculate table's height and width
    
    
    
    long titleNum = _titles.count;
    CGFloat tableHeight = 0;
    CGFloat tableWidth = 0;
    for (int i = 0; i<_leftObjects.count; i++) {
        tableHeight += [self heightForRowAtIndex:i];
    }
    for (int i = 0; i<_titles.count; i++) {
        tableWidth += [self widthForColAtIndex:i];
    }
    
    //setup background scrollview and titles
    float titleHeight = [self heightForTitles];
    float gridWidth = 0;
    if ([self isTitleFixed]) {
        //setup scrollview for titles
        _backTitleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, titleHeight)];
        _backTitleScrollView.scrollEnabled = YES;
        _backTitleScrollView.contentSize = CGSizeMake(tableWidth, titleHeight);
        _backTitleScrollView.delegate = self;
        _backTitleScrollView.bounces = NO;
        
        //setup titles
        float offsetX = 0;
        for (long i = 0; i < _titles.count; i++) {
            gridWidth = [self widthForColAtIndex:i];
            CGRect frame = CGRectMake(offsetX, 0, gridWidth, titleHeight);
            offsetX += gridWidth;
            UIView *titleView = [[UIView alloc] initWithFrame:frame];
            titleView.layer.borderWidth = 0.5;
            titleView.layer.borderColor = [[UIColor groupTableViewBackgroundColor] CGColor];
            titleView.backgroundColor = [self backgroundColorForTitleAtIndex:i];
            UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
            titleLbl.text = [[_titles objectAtIndex:i] valueForKey:_titleKey];
            titleLbl.textColor = [self textColorForTitleAtIndex:i];
            [titleLbl setFont:[self fontSizeForTitleAtIndex:i]];
            titleLbl.numberOfLines = 0;
            titleLbl.lineBreakMode = NSLineBreakByWordWrapping;
            [self applyAlignmentTypeFor:titleLbl];
            [titleView addSubview:titleLbl];
            [_backTitleScrollView addSubview:titleView];
        }
        
        //setup scrollview
        _backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, titleHeight, self.frame.size.width, self.frame.size.height-titleHeight)];
        _backScrollView.scrollEnabled = YES;
        _backScrollView.delegate = self;
        _backScrollView.bounces = NO;
        [self addSubview:_backTitleScrollView];
        [self addSubview:_backScrollView];
    }else if (![self isTitleFixed]) {
        //setup scrollview
        _backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _backScrollView.scrollEnabled = YES;
        _backScrollView.bounces = NO;
        
        //setup titles
        float offsetX = 0;
        for (long i = 0; i < _titles.count; i++) {
            gridWidth = [self widthForColAtIndex:i];
            CGRect frame = CGRectMake(offsetX, 0, gridWidth, titleHeight);
            offsetX += gridWidth;
            UIView *titleView = [[UIView alloc] initWithFrame:frame];
            titleView.layer.borderWidth = 0.5;
            titleView.layer.borderColor = [[UIColor groupTableViewBackgroundColor] CGColor];
            titleView.backgroundColor = [self backgroundColorForTitleAtIndex:i];
            UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
            titleLbl.text = [[_titles objectAtIndex:i] valueForKey:_titleKey];
            titleLbl.textColor = [self textColorForTitleAtIndex:i];
            [titleLbl setFont:[self fontSizeForTitleAtIndex:i]];
            titleLbl.numberOfLines = 0;
            titleLbl.lineBreakMode = NSLineBreakByWordWrapping;

            [self applyAlignmentTypeFor:titleLbl];
            [titleView addSubview:titleLbl];
            [_backScrollView addSubview:titleView];
        }
        [self addSubview:_backScrollView];
    }
    
    //setup tables
    float offsetX = 0;
    for (long i = 0; i < titleNum; i++) {
        CGRect frame = CGRectZero;
        gridWidth = [self widthForColAtIndex:i];
        if ([self isTitleFixed]) {
            frame = CGRectMake(offsetX, 0, gridWidth, tableHeight);
        }else{
            frame = CGRectMake(offsetX, [self heightForTitles], gridWidth, tableHeight);
        }
        offsetX += gridWidth;
        UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        tableView.layer.borderWidth = 0.5;
        tableView.layer.borderColor = [[UIColor groupTableViewBackgroundColor] CGColor];
        tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        tableView.scrollEnabled = NO;
        tableView.tag = i;
        tableView.delegate = self;
        tableView.dataSource = self;
        [_backScrollView addSubview:tableView];
    }
    
    //setup scrollview's content size
    float contentWidth = tableWidth;
    float contentHeight = [self isTitleFixed]?tableHeight:tableHeight + [self heightForTitles];
    [_backScrollView setContentSize:CGSizeMake(contentWidth, [self isTitleFixed]?tableHeight:contentHeight)];
}

#pragma mark --Set Up Methods
- (void)applyAlignmentTypeFor:(UILabel *)label{
    switch ([self gridViewAlignmentType]) {
        case JHGridAlignmentTypeDefault:
        case JHGridAlignmentTypeCenter:
            label.textAlignment = NSTextAlignmentCenter;
            break;
        case JHGridAlignmentTypeLeft:
            label.textAlignment = NSTextAlignmentLeft;
            break;
        case JHGridAlignmentTypeRight:
            label.textAlignment = NSTextAlignmentRight;
            break;
        default:
            break;
    }
    
}

- (void)applyAlignmentTypeFor:(UILabel *)label gridIndex:(GridIndex)gridIndex{
    switch ([self gridViewAlignmentTypeWithGridIndex:gridIndex]) {
        case JHGridAlignmentTypeDefault:
        case JHGridAlignmentTypeCenter:
            label.textAlignment = NSTextAlignmentCenter;
            break;
        case JHGridAlignmentTypeLeft:
            label.textAlignment = NSTextAlignmentLeft;
            break;
        case JHGridAlignmentTypeRight:
            label.textAlignment = NSTextAlignmentRight;
            break;
        default:
            break;
    }
    
}


#pragma mark --Self TableView Delegate Methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellId = @"JHGridTableViewCell";
    JHGridTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        cell = [[JHGridTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.jhDelegate = self;
    
    if (self.type == 2) {
        cell.textField.enabled = NO;
    }else {
        cell.textField.enabled  =YES;
    }
    //setup content of cells
    long index = indexPath.row;
    
    if (tableView.tag == 0) {
        
        cell.textLabel.text = [[_leftObjects objectAtIndex:index] valueForKey:_leftKey];

    }else {
        
        
        if (self.isSaved == 0) {
            
            for (int i = 1; i < _titles.count  ; i ++) {
                NSArray *array = _tags[i - 1];
                for (int j = 0; j < array.count; j ++) {
                    Model_Hr_InspectNew *model = array[j];
                    model.CODE_ID = [[_titles objectAtIndex:i] valueForKey:@"CODE_ID"];
                    model.BUCKLESCORE = @"";
                    model.RESULT_ID = self.dateString;
                    model.CONTENT_ID = [[_titles objectAtIndex:i] valueForKey:@"CONTENT_ID"];
                    if (j == 0) {
                        model.ITEM_ID = @"ALL";
                    }else if (j == 1) {
                        model.ITEM_ID = @"ALLFHS";
                    }else {
                        model.ITEM_ID = [[_leftObjects objectAtIndex:j] valueForKey:@"ITEM_ID"];
                        
                    }
                }
            }

 
        }else {
            
            if (self.isHasInspect == 0) {
                NSString *CONTENT_ID = [[_titles objectAtIndex:tableView.tag] valueForKey:@"CONTENT_ID"];
                NSString *ITEM_ID = [[_leftObjects objectAtIndex:indexPath.row] valueForKey:@"ITEM_ID"];
                for (NSArray *array in _tags) {
                    for (Model_Hr_InspectNew *model in array) {
                        if ([CONTENT_ID isEqualToString:model.CONTENT_ID] && [ITEM_ID isEqualToString:model.ITEM_ID]) {
                            cell.textField.text = model.QUANTITY?:@"";
                            
                        }
                    }
                }
                
            }else {
                
                for (int i = 1; i < _titles.count  ; i ++) {
                    NSArray *array = _tags[i - 1];
                    for (int j = 0; j < array.count; j ++) {
                        Model_Hr_InspectNew *model = array[j];
                        model.CODE_ID = self.code_id;
                        model.BUCKLESCORE = @"";
                        model.RESULT_ID = self.dateString;
                        model.CONTENT_ID = [[_titles objectAtIndex:i] valueForKey:@"CONTENT_ID"];
                        if (j == 0) {
                            model.ITEM_ID = @"ALL";
                        }else if (j == 1) {
                            model.ITEM_ID = @"ALLFHS";
                        }else {
                            model.ITEM_ID = [[_leftObjects objectAtIndex:j] valueForKey:@"ITEM_ID"];
                            
                        }
                    }
                }

            }

        }
        
    }
    
    GridIndex gridIndex;
    gridIndex.col = tableView.tag;
    gridIndex.row = indexPath.row;
    cell.backgroundColor = [self backgroundColorForGridAtGridIndex:gridIndex];
    cell.textLabel.textColor = [self textColorForGridAtGridIndex:gridIndex];
    [cell.textLabel setFont:[self fontSizeForGridAtGridIndex:gridIndex]];
    cell.textLabel.hidden = ![self isHiddenViewForGridAtGridIndex:gridIndex];
    cell.textField.hidden = [self isHiddenViewForGridAtGridIndex:gridIndex];
    
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [self applyAlignmentTypeFor:cell.textLabel gridIndex:gridIndex];
    
    [cell arrangeCellIndex:gridIndex.col row:gridIndex.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GridIndex gridIndex;
    gridIndex.col = tableView.tag;
    gridIndex.row = indexPath.row;
    
    [self didSelectRowAtGridIndex:gridIndex];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _leftObjects.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndex:(NSIndexPath *)indexPath{
    return [self heightForRowAtIndex:indexPath.row];
}

#pragma mark --Self ScrollView Delegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([scrollView isEqual:_backTitleScrollView]) {
        CGPoint offset = _backScrollView.contentOffset;
        offset.x = _backTitleScrollView.contentOffset.x;
        [_backScrollView setContentOffset:offset];
    }else{
        CGPoint offset = _backTitleScrollView.contentOffset;
        offset.x = _backScrollView.contentOffset.x;
        [_backTitleScrollView setContentOffset:offset];
    }
}

#pragma mark --JHGridView Delegate Methods
- (CGFloat)heightForTitles{
    if([_delegate respondsToSelector:@selector(heightForTitles)]){
        return [_delegate heightForTitles];
    }else{
        return 44;
    }
}

- (BOOL)isTitleFixed{
    if([_delegate respondsToSelector:@selector(isTitleFixed)]){
        return [_delegate isTitleFixed];
    }else{
        return NO;
    }
}

- (BOOL)isRowSelectable{
    if([_delegate respondsToSelector:@selector(isRowSelectable)]){
        return [_delegate isRowSelectable];
    }else{
        return NO;
    }
}

- (CGFloat)heightForRowAtIndex:(long)index{
    if([_delegate respondsToSelector:@selector(heightForRowAtIndex:)]){
        return [_delegate heightForRowAtIndex:index];
    }else{
        return 44;
    }
}

- (CGFloat)widthForColAtIndex:(long)index{
    if([_delegate respondsToSelector:@selector(widthForColAtIndex:)]){
        return [_delegate widthForColAtIndex:index];
    }else{
        return 90;
    }
}

- (void)didSelectRowAtGridIndex:(GridIndex)gridIndex{

    if([_delegate respondsToSelector:@selector(didSelectRowAtGridIndex:)]){
        [_delegate didSelectRowAtGridIndex:gridIndex];
    }
}

- (JHGridSelectType)gridViewSelectType{
    if ([_delegate respondsToSelector:@selector(gridViewSelectType)]) {
        return [_delegate gridViewSelectType];
    }else{
        return JHGridSelectTypeDefault;
    }
}

- (JHGridAlignmentType)gridViewAlignmentType{
    if ([_delegate respondsToSelector:@selector(gridViewSelectType)]) {
        return [_delegate gridViewAlignmentType];
    }else{
        return JHGridAlignmentTypeCenter;
    }
}

- (JHGridAlignmentType)gridViewAlignmentTypeWithGridIndex:(GridIndex)gridIndex{
    if ([_delegate respondsToSelector:@selector(gridViewAlignmentTypeWithGridIndex:)]) {
        return [_delegate gridViewAlignmentTypeWithGridIndex:gridIndex];
    }else{
        return JHGridAlignmentTypeCenter;
    }
}

- (UIColor *)backgroundColorForTitleAtIndex:(long)index{
    if ([_delegate respondsToSelector:@selector(backgroundColorForTitleAtIndex:)]) {
        return [_delegate backgroundColorForTitleAtIndex:index];
    }else{
        return JHGridSelectTypeDefault;
    }
}

- (UIColor *)backgroundColorForGridAtGridIndex:(GridIndex)gridIndex{
    if ([_delegate respondsToSelector:@selector(backgroundColorForGridAtGridIndex:)]) {
        return [_delegate backgroundColorForGridAtGridIndex:gridIndex];
    }else{
        return [UIColor whiteColor];
    }
}

- (UIColor *)textColorForTitleAtIndex:(long)index{
    if ([_delegate respondsToSelector:@selector(textColorForTitleAtIndex:)]) {
        return [_delegate textColorForTitleAtIndex:index];
    }else{
        return JHGridSelectTypeDefault;
    }
}

- (UIColor *)textColorForGridAtGridIndex:(GridIndex)gridIndex{
    if ([_delegate respondsToSelector:@selector(textColorForGridAtGridIndex:)]) {
        return [_delegate textColorForGridAtGridIndex:gridIndex];
    }else{
        return [UIColor whiteColor];
    }
}
- (UIFont *)fontSizeForTitleAtIndex:(long)index{
    if ([_delegate respondsToSelector:@selector(fontForTitleAtIndex:)]) {
        return [_delegate fontForTitleAtIndex:index];
    }else{
        return [UIFont systemFontOfSize:17];
    }
}

- (UIFont *)fontSizeForGridAtGridIndex:(GridIndex)gridIndex{
    if ([_delegate respondsToSelector:@selector(fontForGridAtGridIndex:)]) {
        return [_delegate fontForGridAtGridIndex:gridIndex];
    }else{
        return [UIFont systemFontOfSize:17];
    }
}

- (BOOL)isHiddenViewForGridAtGridIndex:(GridIndex)gridIndex {
    if ([_delegate respondsToSelector:@selector(hiddenViewForGridAtGridIndex:)]) {
        return [_delegate hiddenViewForGridAtGridIndex:gridIndex];
    }else{
        return YES;
    }
}

- (void)workFlowsTableViewCell:(JHGridTableViewCell *)cell textFileText:(NSString *)text section:(long)section row:(long)row {
    
    
    NSArray *array = _tags[section - 1];
    Model_Hr_InspectNew *model = array[row];
    
    if (text.length == 0) {
        text = @"";
    }
    model.QUANTITY = text;
    
   
        
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFileText:section:row:array:)]) {
        [self.delegate textFileText:text section:section row:row array:_tags];
    }
}

@end

